//
//  LDY_VoiceViewController.m
//  VoiceProject
//
//  Created by dlios on 15/11/24.
//  Copyright © 2015年 lulu. All rights reserved.
//

#import "LDY_VoiceViewController.h"
#import "Network.h"
#import "LDY_VoiceModel.h"
#import "LDY_VoiceCollectionViewCell.h"
#import "LDY_VoiceTableViewCell.h"
#import <MJRefresh.h>
#import "LDY_PlayViewController.h"
#import "DatabaseHandler.h"
#import "CXFirstDetailModel.h"
#import "FZ_Dounment.h"
#import "CoreDataManager.h"
#define KWIDTH ([UIScreen mainScreen].bounds.size.width/375)
#define KHEIGHT ([UIScreen mainScreen].bounds.size.height/667)

@interface LDY_VoiceViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UIButton *recommendbutton;

@property (nonatomic, retain) UIButton *hotbutton;

@property (nonatomic, retain) UICollectionView *recommendCollectionView;

@property (nonatomic, retain) UICollectionView *hotCollectionView;

@property (nonatomic, retain) UITableView *hotTableView;

@property (nonatomic, retain) NSMutableArray *recommendDataArr;

@property (nonatomic, retain) NSMutableArray *recommendMainArr;

@property (nonatomic, retain) NSMutableArray *hotDataArr1;

@property (nonatomic, retain) NSMutableArray *hotMainArr1;

@property (nonatomic, retain) NSMutableArray *hotDataArr2;

@property (nonatomic, retain) NSMutableArray *hotMainArr2;

@property (nonatomic, retain) NSMutableArray *hotDataArr3;

@property (nonatomic, retain) NSMutableArray *hotMainArr3;

@property (nonatomic, assign) NSInteger *hotPage;

@property (nonatomic, assign) NSInteger *recommendPage;

@property (nonatomic, assign) NSInteger *hotMaxPage;

@property (nonatomic, assign) NSInteger *recommendMaxPage;

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain)NSArray *tempArray;
@property (nonatomic, retain)CoreDataManager *manager;


@end

@implementation LDY_VoiceViewController
-(void)dealloc{
    [_recommendbutton release];
    [_hotbutton release];
    [_recommendCollectionView release];
    [_hotCollectionView release];
    [_hotTableView release];
    [_recommendDataArr release];
    [_recommendMainArr release];
    [_hotDataArr1 release];
    [_hotMainArr1 release];
    [_hotDataArr2 release];
    [_hotMainArr2 release];
    [_hotDataArr3 release];
    [_hotMainArr3 release];
    [_scrollView release];
    [_tempArray release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        self.recommendDataArr = [NSMutableArray array];
        self.recommendMainArr = [NSMutableArray array];
        
        self.hotDataArr1 = [NSMutableArray array];
        self.hotMainArr1 = [NSMutableArray array];
        
        self.hotDataArr2 = [NSMutableArray array];
        self.hotMainArr2 = [NSMutableArray array];
        
        self.hotDataArr3 = [NSMutableArray array];
        self.hotMainArr3 = [NSMutableArray array];
        
        self.recommendPage = 0;
        self.hotPage = 0;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.automaticallyAdjustsScrollViewInsets = NO;
    self.manager = [CoreDataManager shareManager];
    
    [self createButton];
    
    [self createScrollView];

    [self createButton];
    
    [self commendDataHandle];
    
    [self hotDataHandle];
    
    [self createCommendCollectionView];
    
    [self createHotTableView];
    
    [self createHotCollectionView];
    
    [self createPlayButton];

}

-(void)createPlayButton{
    UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [playButton setFrame:CGRectMake(self.view.frame.size.width - 40, self.recommendbutton.frame.origin.y, 20, self.recommendbutton.frame.size.height)];
    [playButton addTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside];
//    [playButton setBackgroundColor: [UIColor redColor]];
    [playButton setBackgroundImage:[UIImage imageNamed:@"iconfont-yuleguangdieblack.png"] forState:UIControlStateNormal];
    [self.navigationController.navigationBar addSubview:playButton];
}
- (void)createButton
{
    self.recommendbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.navigationController.navigationBar addSubview:self.recommendbutton];
    self.recommendbutton.frame = CGRectMake(KWIDTH * 145, KHEIGHT * 10, KWIDTH * 40, KHEIGHT * 20);
//    self.newbutton.layer.cornerRadius = 3;
//    [self.recommendbutton setBackgroundColor:[UIColor blackColor]];
    [self.recommendbutton setTitle:@"推荐" forState:UIControlStateNormal];
    [self.recommendbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.recommendbutton.titleLabel.font = [UIFont systemFontOfSize:15 * KWIDTH];
    [self.recommendbutton addTarget:self action:@selector(recommendbuttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    self.hotbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.navigationController.navigationBar addSubview:self.hotbutton];
    self.hotbutton.frame = CGRectMake(KWIDTH * 190, KHEIGHT * 10, KWIDTH * 40, KHEIGHT * 20);
//    self.hotbutton.layer.cornerRadius = 3;
//    [self.hotbutton setBackgroundColor:[UIColor blackColor]];
    [self.hotbutton setTitle:@"热门" forState:UIControlStateNormal];
    [self.hotbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//    self.hotbutton.alpha = 0.3;
    self.hotbutton.titleLabel.font = [UIFont systemFontOfSize:15 * KWIDTH];
    [self.hotbutton addTarget:self action:@selector(hotbuttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)createScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH * 375, KHEIGHT * 667)];
    
    self.scrollView.backgroundColor = [UIColor grayColor];
    
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, 0);
    
    [self.view addSubview:self.scrollView];
    
    /* 整页滚动 */
    self.scrollView.pagingEnabled = YES;
    
    /* 视图左上角距离原点的偏移量 */
    self.scrollView.contentOffset = CGPointMake(0, 0);
    
    /* 是否显示垂直滚动条, 默认值为YES */
    self.scrollView.showsVerticalScrollIndicator = NO;
    
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    /* 边界是否回弹 */
    self.scrollView.bounces = NO;
    
    self.scrollView.delegate = self;
    
    [_scrollView release];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([[NSString stringWithFormat:@"%@",[scrollView class]] isEqualToString:@"UIScrollView"]){
        
        if (scrollView.contentOffset.x == KWIDTH * 375) {
            
            [self.recommendbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [self.hotbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
        }else if(scrollView.contentOffset.x == 0) {
            
            [self.recommendbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.hotbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
    }
}

-(void)playButton:(UIButton *)button{
    LDY_PlayViewController *play = [LDY_PlayViewController sharePlayer];
    play.hidesBottomBarWhenPushed = YES;
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController pushViewController:play animated:YES];
    
    //暂时没释放
    //    [play release];
}

- (void)recommendbuttonClicked:(UIButton *)button
{
    [self.recommendbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.hotbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    self.scrollView.contentOffset = CGPointMake(0, 0);
}

-(void)hotbuttonClicked:(UIButton *)button
{
    [self.recommendbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.hotbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    self.scrollView.contentOffset = CGPointMake(KWIDTH * 375, 0);
}



- (void)commendDataHandle
{
    [Network networkGETRequesWithURL:@"http://echosystem.kibey.com/sound/hot?page=1" Paramater:nil pageUniquenessIdentifier:@"http://echosystem.kibey.com/sound/hot?page=1" result:^(id result) {
        
        self.recommendDataArr = [[result objectForKey:@"result"] objectForKey:@"data"];
        
//        NSLog(@"%@", self.dataArr);
        
        for (NSDictionary *dic in self.recommendDataArr) {
            
            LDY_VoiceModel *model = [LDY_VoiceModel modelWithDictionary:dic];
            
            [self.recommendMainArr addObject:model];
        }
        
//        NSLog(@"%@", self.recommendMainArr);
        
        if (self.recommendMainArr.count > 0) {
            
            [self.recommendCollectionView reloadData];
        }
        
    }];
}

- (void)hotDataHandle
{
    NSString *url = @"http://echosystem.kibey.com/hot/sounds";
    
    [Network networkPOSTRequestWithURL:url body:@"v=9&period=daily" pageUniquenessIdentifier:@"v=9&period=daily" result:^(id result) {
        
        self.hotDataArr1 = [[result objectForKey:@"result"] objectForKey:@"data"];
        
        for (NSDictionary *dic in self.hotDataArr1) {
            
            LDY_VoiceModel *model = [LDY_VoiceModel modelWithDictionary:dic];
            
            [self.hotMainArr1 addObject:model];
        }
        if (self.hotMainArr1.count > 0) {
            
//            [self.hotTableView reloadData];
            [self.hotCollectionView reloadData];
        }
    }];
    
    [Network networkPOSTRequestWithURL:url body:@"v=9&period=weekly" pageUniquenessIdentifier:@"v=9&period=weekly" result:^(id result) {
       
        self.hotDataArr2 = [[result objectForKey:@"result"] objectForKey:@"data"];
        
        for (NSDictionary *dic in self.hotDataArr2) {
            
            LDY_VoiceModel *model = [LDY_VoiceModel modelWithDictionary:dic];
            
            [self.hotMainArr2 addObject:model];
        }
        if (self.hotMainArr2.count > 0) {
            
            [self.hotTableView reloadData];
            [self.hotCollectionView reloadData];

        }
        
    }];
    
    [Network networkPOSTRequestWithURL:url body:@"v=9&period=monthly" pageUniquenessIdentifier:@"v=9&period=monthly" result:^(id result) {
        
        self.hotDataArr3 = [[result objectForKey:@"result"] objectForKey:@"data"];
        
        for (NSDictionary *dic in self.hotDataArr3) {
            
            LDY_VoiceModel *model = [LDY_VoiceModel modelWithDictionary:dic];
            
            [self.hotMainArr3 addObject:model];
        }
        if (self.hotMainArr3.count > 0) {
            
            [self.hotTableView reloadData];
            [self.hotCollectionView reloadData];

        }
        
    }];
    
}


- (void)createCommendCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    self.recommendCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH * 375, KHEIGHT * 667 - 50) collectionViewLayout:flowLayout];
    [self.scrollView addSubview:self.recommendCollectionView];
    
    self.recommendCollectionView.backgroundColor = [UIColor whiteColor];
    
    self.recommendCollectionView.delegate = self;
    self.recommendCollectionView.dataSource = self;
    
    self.recommendCollectionView.tag = 1000;
    
    [self.recommendCollectionView release];
    
    [self.recommendCollectionView registerClass:[LDY_VoiceCollectionViewCell class] forCellWithReuseIdentifier:@"LDY_VoiceCollectionViewCell1"];
    
    self.recommendCollectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(recommendHeaderFreshing)];
    
    self.recommendCollectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(recommendFooterFreshing)];
    
}

- (void)recommendHeaderFreshing
{
    [self.recommendMainArr removeAllObjects];
    
    [self commendDataHandle];
    
    [self.recommendCollectionView.header endRefreshing];
}


- (void)recommendFooterFreshing
{
    self.recommendPage++;
    
    NSString *url = [NSString stringWithFormat:@"http://echosystem.kibey.com/sound/hot?page=%ld", self.recommendPage];
    
    [Network networkGETRequesWithURL:url Paramater:nil pageUniquenessIdentifier:url result:^(id result) {
        
        self.recommendDataArr = [[result objectForKey:@"result"] objectForKey:@"data"];
        
        //        NSLog(@"%@", self.dataArr);
        
        for (NSDictionary *dic in self.recommendDataArr) {
            
            LDY_VoiceModel *model = [LDY_VoiceModel modelWithDictionary:dic];
            
            [self.recommendMainArr addObject:model];
        }
        
        //        NSLog(@"%@", self.recommendMainArr);
        
        if (self.recommendMainArr.count > 0) {
            
            [self.recommendCollectionView reloadData];
        }
    
    }];
    
    [self.recommendCollectionView.footer endRefreshing];
    
    if (self.recommendMaxPage) {
        NSLog(@"%ld, %ld", (long)self.recommendPage, (long)self.recommendMaxPage);
        self.recommendCollectionView.footer.hidden = YES;
    }
}


- (void)createHotTableView
{
    self.hotTableView = [[UITableView alloc] initWithFrame:CGRectMake(KWIDTH * 375, 0, KWIDTH * 375, KHEIGHT * 667 - KHEIGHT * 60) style:UITableViewStyleGrouped];
    
    [self.scrollView addSubview:self.hotTableView];
    
    self.hotTableView.backgroundColor = [UIColor whiteColor];
    
    self.hotTableView.delegate = self;
    self.hotTableView.dataSource = self;
    
    self.hotTableView.rowHeight = KHEIGHT * 80;
    
//    self.hotTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [_hotTableView release];
    

    self.hotTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(hotHeaderFreshing)];
//    [self.hotTableView registerClass:[LDY_VoiceTableViewCell class] forCellReuseIdentifier:@""];
    
}


- (void)hotHeaderFreshing
{
    [self.hotMainArr1 removeAllObjects];
    [self.hotMainArr2 removeAllObjects];
    [self.hotMainArr3 removeAllObjects];
    
    [self hotDataHandle];
    
    [self.hotTableView.header endRefreshing];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
    
        return 50 * KHEIGHT;
    }else{
        
        return KHEIGHT * 40;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        
        if (self.hotMainArr2.count > 0) {
            
            return self.hotMainArr2.count;
            
        }else{
            
            return 0;
        }
    
    }else{
        
        if (self.hotMainArr3.count > 0) {
            
            return self.hotMainArr3.count;
            
        }else{
            
            return 0;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LDY_VoiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LDY_VoiceTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (!cell) {
        cell = [[LDY_VoiceTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LDY_VoiceTableViewCell"];
    }
    
    if (indexPath.section == 0) {
        
        if (self.hotMainArr2.count > 0) {
            cell.model = [self.hotMainArr2 objectAtIndex:indexPath.row];
        }
        
    }else{
        
        if (self.hotMainArr3.count > 0) {
            cell.model = [self.hotMainArr3 objectAtIndex:indexPath.row];
        }
        
    }
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"本周热门";
    }else{
        
        return @"本月热门";
    }
}

- (void)createHotCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    self.hotCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH * 375, KHEIGHT * 220 * 3) collectionViewLayout:flowLayout];

    self.hotTableView.tableHeaderView = self.hotCollectionView;
    self.hotCollectionView.backgroundColor = [UIColor whiteColor];
    
    self.hotCollectionView.delegate = self;
    self.hotCollectionView.dataSource = self;
    
    self.hotCollectionView.scrollEnabled = NO;
    
    self.hotCollectionView.tag = 2000;
    
    [_hotCollectionView release];
    
    [self.hotCollectionView registerClass:[LDY_VoiceCollectionViewCell class] forCellWithReuseIdentifier:@"LDY_VoiceCollectionViewCell2"];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KWIDTH * 10, KHEIGHT * 15, KWIDTH * 375, KHEIGHT * 30)];
    [self.hotCollectionView addSubview:titleLabel];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.font = [UIFont systemFontOfSize:13.5];
    titleLabel.text = @"今日最热";
    [titleLabel release];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        LDY_PlayViewController *playController = [LDY_PlayViewController sharePlayer];
        
        [[LDY_PlayViewController sharePlayer] setData:self.hotMainArr2 row:indexPath.row];
        LDY_VoiceModel *model = [self.hotMainArr2 objectAtIndex:indexPath.row];
        //传参数
//        playController.mo
        playController.name = model.name;
        playController.pic = model.pic;
        playController.playUrl = model.source;
        playController.indexRow = indexPath.row;
        
        [self searchHistory];
        int count = 0;
        if (self.tempArray.count == 0) {
            FZ_Dounment *list = [NSEntityDescription insertNewObjectForEntityForName:@"FZ_Dounment" inManagedObjectContext:self.manager.managedObjectContext];
            list.name = model.name;
            list.pic = model.pic;
            list.source = model.source;
            [self.manager saveContext];
        }
        else{
            for (FZ_Dounment *modelll in self.tempArray) {
                if (![modelll.source isEqualToString:model.source]) {
                    count++;
                }
            }
            if (count == self.tempArray.count) {
                FZ_Dounment *list = [NSEntityDescription insertNewObjectForEntityForName:@"FZ_Dounment" inManagedObjectContext:self.manager.managedObjectContext];
                list.name = model.name;
                list.pic = model.pic;
                list.source = model.source;
                
                [self.manager saveContext];
            }
        }

        [self.navigationController pushViewController:playController animated:YES];

    }else{
        LDY_PlayViewController *playController = [LDY_PlayViewController sharePlayer];
        
        [[LDY_PlayViewController sharePlayer] setData:self.hotMainArr3 row:indexPath.row];
        LDY_VoiceModel *model = [self.hotMainArr3 objectAtIndex:indexPath.row];

        //传参数
        playController.name = model.name;
        playController.pic = model.pic;
        playController.playUrl = model.source;
        playController.indexRow = indexPath.row;
        [self searchHistory];
        int count = 0;
        if (self.tempArray.count == 0) {
            FZ_Dounment *list = [NSEntityDescription insertNewObjectForEntityForName:@"FZ_Dounment" inManagedObjectContext:self.manager.managedObjectContext];
            list.name = model.name;
            list.pic = model.pic;
            list.source = model.source;
            [self.manager saveContext];
        }
        else{
            for (FZ_Dounment *modelll in self.tempArray) {
                if (![modelll.source isEqualToString:model.source]) {
                    count++;
                }
            }
            if (count == self.tempArray.count) {
                FZ_Dounment *list = [NSEntityDescription insertNewObjectForEntityForName:@"FZ_Dounment" inManagedObjectContext:self.manager.managedObjectContext];
                list.name = model.name;
                list.pic = model.pic;
                list.source = model.source;
                
                [self.manager saveContext];
            }
        }


        [self.navigationController pushViewController:playController animated:YES];
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (collectionView.tag == 1000) {
        return 1;
    }else{
        return 1;
    }
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView.tag == 1000) {
        
        if (self.recommendMainArr.count > 0) {
            return self.recommendMainArr.count;
        }else{
            return 0;
        }
        
    }else{
        
        if (self.hotMainArr1.count > 0) {
            return self.hotMainArr1.count;
        }else{
            return 0;
        }
        
    }
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView.tag == 1000) {
        
        LDY_VoiceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LDY_VoiceCollectionViewCell1" forIndexPath:indexPath];
        
        if (self.recommendMainArr.count > 0) {
            cell.model = [self.recommendMainArr objectAtIndex:indexPath.item];
        }
        
        return cell;
    }else{
        
        LDY_VoiceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LDY_VoiceCollectionViewCell2" forIndexPath:indexPath];
        
        if (self.hotMainArr1.count > 0) {
            
            cell.model = [self.hotMainArr1 objectAtIndex:indexPath.item];
            
        }
        
        return cell;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((KWIDTH * 375 - KWIDTH * 30) / 2, KHEIGHT * 220);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(KHEIGHT * 10, KWIDTH * 8, KHEIGHT * 10, KWIDTH * 8);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (collectionView.tag == 2000) {
        return CGSizeMake(KWIDTH * 375, KHEIGHT * 40);
    }else{
        
        return CGSizeMake(0, KHEIGHT * 20);
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView.tag == 1000) {
        LDY_PlayViewController *playController = [LDY_PlayViewController sharePlayer];

        [[LDY_PlayViewController sharePlayer] setData:self.recommendMainArr row:indexPath.row];
        LDY_VoiceModel *model = [self.recommendMainArr objectAtIndex:indexPath.row];

        //传参数
        playController.name = model.name;
        playController.pic = model.pic;
        playController.playUrl = model.source;
        playController.indexRow = indexPath.row;
        [self searchHistory];
        int count = 0;
        if (self.tempArray.count == 0) {
            FZ_Dounment *list = [NSEntityDescription insertNewObjectForEntityForName:@"FZ_Dounment" inManagedObjectContext:self.manager.managedObjectContext];
            list.name = model.name;
            list.pic = model.pic;
            list.source = model.source;
            [self.manager saveContext];
        }
        else{
            for (FZ_Dounment *modelll in self.tempArray) {
                if (![modelll.source isEqualToString:model.source]) {
                    count++;
                }
            }
            if (count == self.tempArray.count) {
                FZ_Dounment *list = [NSEntityDescription insertNewObjectForEntityForName:@"FZ_Dounment" inManagedObjectContext:self.manager.managedObjectContext];
                list.name = model.name;
                list.pic = model.pic;
                list.source = model.source;
                
                [self.manager saveContext];
            }
        }

        [self.navigationController pushViewController:playController animated:YES];
    }if (collectionView.tag == 2000) {
        LDY_PlayViewController *playController = [LDY_PlayViewController sharePlayer];
        
        [[LDY_PlayViewController sharePlayer] setData:self.hotMainArr1 row:indexPath.row];
        LDY_VoiceModel *model = [self.hotMainArr1 objectAtIndex:indexPath.row];
        //传参数
        playController.name = model.name;
        playController.pic = model.pic;
        playController.playUrl = model.source;
        playController.indexRow = indexPath.row;
        [self searchHistory];
        int count = 0;
        if (self.tempArray.count == 0) {
            FZ_Dounment *list = [NSEntityDescription insertNewObjectForEntityForName:@"FZ_Dounment" inManagedObjectContext:self.manager.managedObjectContext];
            list.name = model.name;
            list.pic = model.pic;
            list.source = model.source;
            [self.manager saveContext];
        }
        else{
            for (FZ_Dounment *modelll in self.tempArray) {
                if (![modelll.source isEqualToString:model.source]) {
                    count++;
                }
            }
            if (count == self.tempArray.count) {
                FZ_Dounment *list = [NSEntityDescription insertNewObjectForEntityForName:@"FZ_Dounment" inManagedObjectContext:self.manager.managedObjectContext];
                list.name = model.name;
                list.pic = model.pic;
                list.source = model.source;
                
                [self.manager saveContext];
            }
        }

        [self.navigationController pushViewController:playController animated:YES];

    }
}
-(void)searchHistory{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"FZ_Dounment" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"source"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    NSError *error = nil;
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    self.tempArray = fetchedObjects;
    NSLog(@"%@", self.tempArray);
    if (fetchedObjects == nil) {
        
    }else{
        
    }
    
    
}


-(void)viewWillAppear:(BOOL)animated{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];

    self.tabBarController.tabBar.hidden = NO;
//    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
